﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing;
using System.Windows.Forms;
using System.Collections;
using WindowsFormsApplication1.FiltryJG;
using WindowsFormsApplication1.Filtry;

namespace WindowsFormsApplication1
{
    public class Pnm
    {

        public Bitmap bitmapOryginal = null;
        private Bitmapp BITMAPA { get; set; }
        public Bitmap BitmapFiltr { get; set; }
        public Bitmap bitmap = null;
        private Bitmap copyBitmap = null;
        private Bitmap histogramBitmap = null;
        private Bitmap bitmapTB = null;
        private Bitmap bitmapTC = null;
        private Bitmap bitmapTG = null;
        private String comment = "";
        private int width = 0, height = 0;
        private int typeOfFile = 0;
        private int maxValue = -1;
        private int[] histogram;
        private int[] cummulatedHistogram;
        private int[] lut = new int[256];
        private int[] lutContrast = new int[256];
        private int[] lutGamma = new int[256];

        #region region settery i gettery
        public int getWidth()
        {
            return this.width;
        }

        public int getHeight()
        {
            return this.height;
        }

        public String getComment()
        {
            return this.comment;
        }

        public int getTypeOfFile()
        {
            return this.typeOfFile;
        }

        public int TypeOfFile
        {
            get
            {
                return this.typeOfFile;
            }
            set
            {
                typeOfFile = value;
            }
        }


        public int getMaxValue()
        {
            return this.maxValue;
        }

        public Bitmap getBitmap()
        {
            return bitmap;
        }

        public Bitmap getHistogramBitmap()
        {
            return histogramBitmap;
        }

        public Bitmap Bitmap
        {
            get
            {
                return bitmap;
            }
            set//seter do undo
            {
                bitmap = value;
            }
        }

        public Bitmap BitmapTB
        {
            get
            {
                return bitmapTB;
            }
        }

        public Bitmap BitmapTC
        {
            get
            {
                return bitmapTC;
            }
        }

        public Bitmap BitmapTG
        {
            get
            {
                return bitmapTG;
            }
        }

        public Bitmap CopyBitmap
        {
            get
            {
                return copyBitmap;
            }
            //set
            //{
            //    bitmap = value;
            //}
        }

        public Bitmap HistogramBitmap
        {
            get
            {
                return this.histogramBitmap;
            }
        }

        public Bitmap Histogram
        {
            get
            {
                return histogramBitmap;
            }
        }

        public int GetWidth
        {
            get
            {
                return width;
            }
        }

        public int GetHeight
        {
            get
            {
                return height;
            }
        }
        #endregion

        public void LoadFile(Stream s)
        {
            int b;
            if (s.ReadByte() == 'P')
            {
                b = s.ReadByte(); //?
                Console.WriteLine(b);
                switch (b) 
                {
                    case '1': ReadP1(s); break;
                    case '2': ReadP2(s); break;
                    case '3': ReadP3(s); break;
                    case '4': ReadP4(s); break;
                    case '5': ReadP5(s); break;
                    case '6': ReadP6(s); break;
                    default: MessageBox.Show("WRONG TYPE OF FILE, NOT P1-P6!"); break;
                }


            }
            else
            {
                MessageBox.Show("WRONG TYPE OF FILE, NOT P1-P6!");
            }
 
        }
        public void ExportFile(Stream s, String filePath)
        {
            int type = this.typeOfFile;
            switch (type)
            {
                case 1: SaveP1(s, filePath); break;
                case 2: SaveP2(s, filePath); break;
                case 3: SaveP3(s, filePath); break;
                case 4: SaveP4(s, filePath); break;
                case 5: SaveP5(s, filePath); break;
                case 6: SaveP6(s, filePath); break;
                default: MessageBox.Show("USZKODZONY TYP PLIKU, NIE MA ZAIMPLEMENTOWANEJ OBSŁUGI ZAPISU TEGO TYPU!"); break;
            }


 
        }

        private void ReadHeader(Stream s)
        {
            String temp= "";
            int b, mode = 0;
            Console.WriteLine(s.ReadByte());
            bool start = true;
            while (start) 
            {
                switch (mode) 
                {
                    case 0: 
                        {   //to jest tryb gdy nie wiemy co nas czeka, możemy być na początku pliku lub odczytywać niedługo dane
                            b = s.ReadByte();
                            if ((b == '\n') || (b == '\r') || (b == '\t') || (b == ' ')) { }//nie wiem czemu...:(
                            else
                            {
                                if (b != '#')
                                {
                                    mode = 2; //mode = 2 to jest tryb wczytywania długości
                                    //temp += b;
                                    temp += Convert.ToChar(Convert.ToInt32(b));
                                }
                                else
                                {
                                    mode = 1; // mode = 1 to jest tryb komentarza
                                }
                            }
                        } break;
                    case 1:
                        {   //tryb komentarza
                            b = s.ReadByte();
                            if ((b == '\n') || (b == '\r'))
                            {
                                mode = 0; // mode = 0 to jest tryb gdzie nie wiemy co nas dalej czeka
                                this.comment += temp;
                                temp = "";
                            }
                            else 
                            {
                                //temp += b; 
                                temp += Convert.ToChar(Convert.ToInt32(b));
                            }
                        } break;
                    case 2:
                        {
                            b = s.ReadByte();
                            if ((b == '\n') || (b == '\r') || (b == '\t') || (b == ' '))
                            {
                                mode = 3; // wczytywanie wysokości
                                this.width = int.Parse(temp);
                                //this.width = Convert.ToInt16(temp);
                                temp = "";
                            }
                            else 
                            {
                                //temp += b;
                                temp += Convert.ToChar(Convert.ToInt32(b));
                            }

                        } break;
                    case 3:
                        {
                            b = s.ReadByte();
                            if ((b == '\n') || (b == '\r') || (b == '\t') || (b == ' '))
                            {
                                this.height = int.Parse(temp);
                                temp = "";
                                if ((this.typeOfFile == 1) || (this.typeOfFile == 4))
                                {
                                    start = false;
                                }
                                else 
                                {
                                    mode = 4;
                                }
                            }
                            else
                            {
                                //temp += b;
                                temp += Convert.ToChar(Convert.ToInt32(b));
                            }

                        } break;
                    case 4:
                        {
                            b = s.ReadByte();
                            if ((b == '\n') || (b == '\r') || (b == '\t') || (b == ' '))
                            {
                                start = false;
                                this.maxValue = int.Parse(temp);
                                temp = "";
                            }
                            else
                            {
                                //temp += b;
                                temp += Convert.ToChar(Convert.ToInt32(b));
                            }

                        } break;
                }

            }

        }


        #region region ReadP1-P6
        private void ReadP1(Stream s)
        {
            this.typeOfFile = 1;
            this.ReadHeader(s);
            //TODO: wpisać do bitmap wartości.
            int pixel;
            int oldPixel;
            bool start =true;
            bitmap = new Bitmap(this.width, this.height);
            BITMAPA = new Bitmapp(this.width, this.height);
            for (int i = 0; i < this.height; i++)
                for (int j = 0; j < this.width; j++)
                {
                    start = true;
                    while (start)
                    {
                        pixel = s.ReadByte();
                        if ((pixel != '\n') && (pixel != '\r') && (pixel != '\t') && (pixel != ' '))
                        {
                            oldPixel = pixel;//zamiana zer na jedynki - podpowiedz by Mroczek
                            if (oldPixel == '0')
                                pixel = 1;
                            if (oldPixel == '1')
                                pixel = 0;

                            if (pixel == 1)
                            {
                                //bitmap.SetPixel(j, i, Color.White);
                                bitmap.SetPixel(j, i, Color.FromArgb(255, 255, 255));
                                BITMAPA.SetPixel(j, i, Color.FromArgb(255, 255, 255));
                                start = false;
                            }
                            else
                            {
                                //bitmap.SetPixel(j, i, Color.Black);
                                bitmap.SetPixel(j, i, Color.FromArgb(0, 0, 0));
                                BITMAPA.SetPixel(j, i, Color.FromArgb(0, 0, 0));
                                start = false;
                            }
                        }
                    }
                    
                }
            this.bitmap = BITMAPA.GetBitmap();
            copyBitmap = bitmap;
            
        }

        private void ReadP2(Stream s)
        {
            this.typeOfFile = 2;
            this.ReadHeader(s);
            //TODO: wpisać do bitmap wartości.
            int pixel;
            bool read = true;
            bool gotNumber = false;
            int scale;
            String temp = "";
            bitmap = new Bitmap(this.width, this.height);
            BITMAPA = new Bitmapp(this.width, this.height);
            for (int i = 0; i < this.height; i++)
                for (int j = 0; j < this.width; j++)
                {
                    temp = "";
                    gotNumber = false; // nie przeczytaliśmy nowej liczby, w razie jakby były 2 separatory obok siebie
                    read = true;//start true - jesteśmy w trybie czytania nowej liczby
                    while (read)
                    {
                        pixel = s.ReadByte();
                        if ((pixel != '\n') && (pixel != '\r') && (pixel != '\t') && (pixel != ' '))
                        {
                            temp += Convert.ToChar(Convert.ToInt32(pixel));
                            gotNumber = true;   

                        }
                        else 
                        {
                            if (gotNumber) 
                            {
                                pixel = int.Parse(temp);
                                scale = maxValue;
                                //if (pixel > 0) scale = Convert.ToInt32(Math.Round(Convert.ToDecimal(255 / pixel), 0));
                                bitmap.SetPixel(j, i, Color.FromArgb(pixel, pixel, pixel));
                                BITMAPA.SetPixel(j, i, Color.FromArgb(pixel, pixel, pixel));
                                //bitmap.SetPixel(j, i, Color.FromArgb(pixel));   //nie rgb                        
                            }
                            read = false;
                        }
                    }

                }
            this.bitmap = BITMAPA.GetBitmap();
            copyBitmap = bitmap;
           
        }

        private void ReadP3(Stream s)
        {
            this.typeOfFile = 3;
            this.ReadHeader(s);
            //TODO: wpisać do bitmap wartości.
            int pixel;
            bool read = true;
            bool gotNumbers = false;
            //int scale;
            int count;
            bool readColorR, readColorG, readColorB = false;
            String temp0, temp1, temp2 = "";
            bitmap = new Bitmap(this.width, this.height);
            BITMAPA = new Bitmapp(this.width, this.height);
            for (int i = 0; i < this.height; i++)
                for (int j = 0; j < this.width; j++)
                {
                    temp0 = "";
                    temp1 = "";
                    temp2 = "";
                    count = 0;
                    readColorR = false;
                    readColorG = false; 
                    readColorB = false;
                    gotNumbers = false; // nie przeczytaliśmy nowej liczby, w razie jakby były 2 separatory obok siebie
                    read = true;//start true - jesteśmy w trybie czytania nowej liczby
                    //wczytujemy trójkę wartości R,G,B i potem ustawiamy gotNumber na true
                    //count 0 to jest R, count 1 wczytujemy G, count 2 wczytujemy B
                        while (read)
                        {
                            pixel = s.ReadByte();
                            if ((pixel != '\n') && (pixel != '\r') && (pixel != '\t') && (pixel != ' '))
                            {
                                switch(count)
                                {
                                    case 0:
                                        { temp0 += Convert.ToChar(Convert.ToInt32(pixel)); readColorR = true; }
                                        break;
                                    case 1:
                                        { temp1 += Convert.ToChar(Convert.ToInt32(pixel)); readColorG = true; }
                                        break;
                                    case 2:
                                        { temp2 += Convert.ToChar(Convert.ToInt32(pixel)); readColorB = true; }
                                        break;
                                }
                            }
                                else
                            {
                                if ((count == 0) &&(readColorR))//przeczytaliśmy wartość R, G i B
                                    count = 1;
                                if ((count == 1) &&(readColorG))
                                    count = 2;
                                if ((count == 2) && (readColorB))
                                {
                                    gotNumbers = true; //Mamy R,G,B i malujemy pixele
                                    read = false;
                                }

                            }
                            
                        }
                    
                            
                                if (gotNumbers)
                                {
                                    //pixel = int.Parse(temp);
                                    //scale = maxValue;

                                    //if (pixel > 0) scale = Convert.ToInt32(Math.Round(Convert.ToDecimal(255 / pixel), 0));

                                    bitmap.SetPixel(j, i, Color.FromArgb(255, int.Parse(temp0), int.Parse(temp1), int.Parse(temp2)));
                                    BITMAPA.SetPixel(j, i, Color.FromArgb(255, int.Parse(temp0), int.Parse(temp1), int.Parse(temp2))); 
                                    read = false;
                                    gotNumbers = false;
                                    readColorR = false;
                                    readColorG = false;
                                    readColorB = false;

                                }
                                
                       
                    

                }
            this.bitmap = BITMAPA.GetBitmap();
            copyBitmap = bitmap;
            
        }

        private void ReadP4(Stream s) //bit-bin
        {
            this.typeOfFile = 4;
            this.ReadHeader(s);
            byte oneByte;
            bool pixel;
            BitArray bitArray = new BitArray(0);//musiałem zainicjować, bo się wysypywało, a i tak to nadpisze
            bool newByte = false;
            int position = 7;

            BinaryReader binaryReader = new BinaryReader(s);
            bitmap = new Bitmap(this.width, this.height);
            BITMAPA = new Bitmapp(this.width, this.height);
            for (int i = 0; i < this.height; i++)
            {
                position = -1;
                for (int j = 0; j < this.width; j++)
                {
                    if (position == -1)
                    {
                        position = 7;
                        newByte = true;
                    }

                    if (newByte)
                    {
                        oneByte = binaryReader.ReadByte();
                        bitArray = new BitArray(new byte[] { oneByte });
                        newByte = false;
                    }

                    pixel = bitArray.Get(position);
                    if (!pixel)//inwersja by Mroczek
                    {
                        bitmap.SetPixel(j, i, Color.FromArgb(255, 255, 255));
                        BITMAPA.SetPixel(j, i, Color.FromArgb(255, 255, 255));
                        position--;
                    }
                    else
                    {
                        bitmap.SetPixel(j, i, Color.FromArgb(0, 0, 0));
                        BITMAPA.SetPixel(j, i, Color.FromArgb(0, 0, 0));
                        position--;
                    }
                }
            }
            this.bitmap = BITMAPA.GetBitmap();
            copyBitmap = bitmap;
           
        }

        private void ReadP5(Stream s) //grey-bin
        {
            this.typeOfFile = 5;
            this.ReadHeader(s);
            byte oneByteRaw;
            int oneByte;
        
            BinaryReader binaryReader = new BinaryReader(s);
            bitmap = new Bitmap(this.width, this.height);
            BITMAPA = new Bitmapp(this.width, this.height);

            for (int i = 0; i < this.height; i++)
            {
                for (int j = 0; j < this.width; j++)
                {
                    oneByteRaw = binaryReader.ReadByte();
                    oneByte = (int)oneByteRaw;
 
                    bitmap.SetPixel(j, i, Color.FromArgb(oneByte, oneByte, oneByte));
                    BITMAPA.SetPixel(j, i, Color.FromArgb(oneByte, oneByte, oneByte));
                }
            }

            this.bitmap = BITMAPA.GetBitmap();
            copyBitmap = bitmap;
          

        }

        private void ReadP6(Stream s)
        {
            this.typeOfFile = 6;
            this.ReadHeader(s);
            byte oneByteRaw;
            int oneByteR, oneByteG, oneByteB;

            BinaryReader binaryReader = new BinaryReader(s);
            bitmap = new Bitmap(this.width, this.height);
            BITMAPA = new Bitmapp(this.width, this.height);
            for (int i = 0; i < this.height; i++)
            {
                for (int j = 0; j < this.width; j++)
                {
                    oneByteRaw = binaryReader.ReadByte();
                    oneByteR = (int)oneByteRaw;
                    oneByteRaw = binaryReader.ReadByte();
                    oneByteG = (int)oneByteRaw;
                    oneByteRaw = binaryReader.ReadByte();
                    oneByteB = (int)oneByteRaw;

                    bitmap.SetPixel(j, i, Color.FromArgb(oneByteR, oneByteG, oneByteB));
                    BITMAPA.SetPixel(j, i, Color.FromArgb(oneByteR, oneByteG, oneByteB));
                }
            }

            this.bitmap = BITMAPA.GetBitmap();
            copyBitmap = bitmap;
         
        }
        #endregion

        private void SaveP1(Stream s, String filePath)
        {
            StreamWriter fileWriter;

            fileWriter = new StreamWriter(filePath, false, Encoding.ASCII);
            fileWriter.Write("P" + Convert.ToString(this.typeOfFile)); // P1
            fileWriter.Write('\n');
            fileWriter.Write("#" + this.comment); // Comment
            fileWriter.Write('\n');
            fileWriter.Write(this.width + " " + this.height); // Wymiary

            Color color;
            for (int i = 0; i < this.height; i++)
            {
                fileWriter.Write('\n');
                for (int j = 0; j < this.width; j++)
                {
                    color = bitmap.GetPixel(j, i);

                    if (color.R == 255)
                    {
                        fileWriter.Write("0");
                    }
                    else
                    {
                        fileWriter.Write("1");
                    }
                }
            }

            fileWriter.Close();            
        }

        private void SaveP2(Stream s, String filePath)
        {
            StreamWriter fileWriter;

            fileWriter = new StreamWriter(filePath, false, Encoding.ASCII);
            fileWriter.Write("P" + Convert.ToString(this.typeOfFile)); // P1
            fileWriter.Write('\n');
            fileWriter.Write("#" + this.comment); // Comment
            fileWriter.Write('\n');
            fileWriter.Write(this.width + " " + this.height); // Wymiary
            fileWriter.Write('\n');
            fileWriter.Write(this.maxValue); // maxValue
            fileWriter.Write('\n');

            Color color;
            for (int i = 0; i < this.height; i++)
            {
                for (int j = 0; j < this.width; j++)
                {
                    color = bitmap.GetPixel(j, i);
                    fileWriter.Write(color.R);
                    fileWriter.Write('\n');
                }
            }

            fileWriter.Close();  
        }

        private void SaveP3(Stream s, String filePath)
        {
            StreamWriter fileWriter;

            fileWriter = new StreamWriter(filePath, false, Encoding.ASCII);
            fileWriter.Write("P" + Convert.ToString(this.typeOfFile)); // P1
            fileWriter.Write('\n');
            fileWriter.Write("#" + this.comment+ '\n'); // Comment
            fileWriter.Write(this.width + " " + this.height); // Wymiary
            fileWriter.Write('\n');
            fileWriter.Write(this.maxValue); // maxValue
            fileWriter.Write('\n');

            Color color;
            for (int i = 0; i < this.height; i++)
            {
                for (int j = 0; j < this.width; j++)
                {
                    color = bitmap.GetPixel(j, i);
                    fileWriter.Write(color.R);
                    fileWriter.Write('\n');
                    fileWriter.Write(color.G);
                    fileWriter.Write('\n');
                    fileWriter.Write(color.B);
                    fileWriter.Write('\n');
                }
            }

            fileWriter.Close();  
        }

        private void SaveP4(Stream s, String filePath)//bit-bin
        {
            StreamWriter fileWriter;
            byte number;

            fileWriter = new StreamWriter(filePath, false, Encoding.ASCII);
            fileWriter.Write("P" + Convert.ToString(this.typeOfFile)); // P1
            fileWriter.Write('\n');
            fileWriter.Write("#" + this.comment); // Comment
            fileWriter.Write('\n');
            fileWriter.Write(this.width + " " + this.height); // Wymiary
            fileWriter.Write('\n');
            fileWriter.Close();

            BinaryWriter binaryWriter;
            binaryWriter = new BinaryWriter(File.Open(@filePath, FileMode.Append));
            int iterator = 7;
            int sum = 0;
            double semiSum;

            byte[] myReadyByte = new byte[8];

            Color color;
            for (int i = 0; i < this.height; i++)
            {
                for (int j = 0; j < this.width; j++)
                {
                    color = bitmap.GetPixel(j, i);
                    number = (byte)(color.R);

                    if (number == 0)
                    {
                        semiSum = Math.Pow(2.0, (double)iterator);
                        sum = sum + Convert.ToInt32(semiSum);
                        iterator--;
                    }
                    else 
                    {
                        iterator--;
                    }
                    if ((iterator == -1) || (j == (this.width-1)))//iterator == -1 przeczytałem 8 bitów
                    {
                        binaryWriter.Write(Convert.ToByte(sum));
                        iterator = 7;
                        sum = 0;

                    }
                }
            }
            binaryWriter.Close();
        }

        private void SaveP5(Stream s, String filePath)
        {
            StreamWriter fileWriter;
            byte number;

            fileWriter = new StreamWriter(filePath, false, Encoding.ASCII);
            fileWriter.Write("P" + Convert.ToString(this.typeOfFile)); // P1
            fileWriter.Write('\n');
            fileWriter.Write("#" + this.comment); // Comment
            fileWriter.Write('\n');
            fileWriter.Write(this.width + " " + this.height); // Wymiary
            fileWriter.Write('\n');
            fileWriter.Write(this.maxValue); // maxValue
            fileWriter.Write('\n');
            fileWriter.Close();

            BinaryWriter binaryWriter;
            binaryWriter = new BinaryWriter(File.Open(@filePath,FileMode.Append));

            Color color;
            for (int i = 0; i < this.height; i++)
            {
                for (int j = 0; j < this.width; j++)
                {
                    color = bitmap.GetPixel(j, i);
                    number = (byte)(color.R);
                    binaryWriter.Write(number);
                }
            }
            binaryWriter.Close();
              
        }

        private void SaveP6(Stream s, String filePath)
        {
            StreamWriter fileWriter;
            byte numberR, numberG, numberB;

            fileWriter = new StreamWriter(filePath, false, Encoding.ASCII);
            fileWriter.Write("P" + Convert.ToString(this.typeOfFile)); // P1
            fileWriter.Write('\n');
            fileWriter.Write("#" + this.comment); // Comment
            fileWriter.Write('\n');
            fileWriter.Write(this.width + " " + this.height); // Wymiary
            fileWriter.Write('\n');
            fileWriter.Write(this.maxValue); // maxValue
            fileWriter.Write('\n');
            fileWriter.Close();

            BinaryWriter binaryWriter;
            binaryWriter = new BinaryWriter(File.Open(@filePath, FileMode.Append));

            Color color;
            for (int i = 0; i < this.height; i++)
            {
                for (int j = 0; j < this.width; j++)
                {
                    color = bitmap.GetPixel(j, i);
                    numberR = (byte)(color.R);
                    numberG = (byte)(color.G);
                    numberB = (byte)(color.B);
                    binaryWriter.Write(numberR);
                    binaryWriter.Write(numberG);
                    binaryWriter.Write(numberB);
                }
            }
            binaryWriter.Close();
              
        }

        public void ComputeHistogramL() 
        {
            Color color;
            int number;
            int numberOfcolors = this.maxValue;
            int sumOfValues = 0;
            int bitmapHeigth = 100;
            int bitmapWidth = 256;
            histogramBitmap = new Bitmap(bitmapWidth, bitmapHeigth);
            float columnHeigth;
            float scaleOfDiagram;

            if ((this.maxValue == -1) || (this.typeOfFile == 1) || (this.typeOfFile == 4)) //P1, P4, maxVaule = -1
            {
                numberOfcolors = 255;
                histogram = new int[numberOfcolors + 1];
                for (int i = 0; i < this.height; i++)
                    for (int j = 0; j < this.width; j++)
                    {
                        color = bitmap.GetPixel(j, i);
                        number = (int)(color.R);
                        if (number == 255)
                            histogram[0]++;
                        else
                            histogram[255]++;
                        sumOfValues++;
                    }
            }

            if ((this.typeOfFile == 2) || (this.typeOfFile == 5))
            {
                histogram = new int[numberOfcolors + 1];//0-255
                for (int i = 0; i < this.height; i++)
                    for (int j = 0; j < this.width; j++)
                    {
                        color = bitmap.GetPixel(j, i);
                        number = (int)(color.R);
                        histogram[number]++;
                        sumOfValues++;
                    }
            }

            if ((this.typeOfFile == 3) || (this.typeOfFile == 6))//odcienie szarości
            {
                histogram = new int[numberOfcolors + 1];//0-255
                for (int i = 0; i < this.height; i++)
                    for (int j = 0; j < this.width; j++)
                    {
                        color = bitmap.GetPixel(j, i);
                        number = (int)(((int)(color.R)) * 0.2989 + ((int)(color.G)) * 0.5870 + ((int)(color.B)) * 0.1140); /// dla kanału L
                        histogram[number]++;
                        sumOfValues++;
                    }
            }

            //dla wszystkich typów
            //skala wykresu 
            scaleOfDiagram = 0;
            for (int i = 0; i < numberOfcolors+1; i++)
            {
                if (histogram[i] > scaleOfDiagram)
                {
                    scaleOfDiagram = histogram[i];
                }
            }
            //np. scaleOfDiagram = 410 (410 wystąpien jakiegoś odcienia) to 410 trzeba przeskalować na 100 pixeli wysokości
            scaleOfDiagram = scaleOfDiagram / bitmapHeigth;
            //1 wartość mnożymy jako jakąś skalę

            for (int i = 0; i < numberOfcolors + 1; i++)//szerokosc
            {
                //columnHeigth = (histogram[i] * 100) / sumOfValues;
                columnHeigth = (histogram[i]) / scaleOfDiagram;
                for (int j = 0; j < columnHeigth; j++)//wysokosc 
                {
                    histogramBitmap.SetPixel(i, (99 - j), Color.FromArgb(0, 0, 0));
                }
            }

        }

        public void ComputeCummulatedHistogram()
        {
            int numberOfcolors = this.maxValue;
            int bitmapHeigth = 100;
            int bitmapWidth = 256;
            histogramBitmap = new Bitmap(bitmapWidth, bitmapHeigth);
            float columnHeigth;
            float scaleOfDiagram;
            if (numberOfcolors == -1) numberOfcolors = 255;
            cummulatedHistogram = new int[numberOfcolors + 1];

            //dla wszystkich typów
            //Histogram Skumulowany

            for (int i = 0; i < 256; i++)
                cummulatedHistogram[i] = histogram[i];

            cummulatedHistogram[0] = histogram[0];
            for (int i = 1; i < 256; i++)
                cummulatedHistogram[i] = cummulatedHistogram[i] + cummulatedHistogram[i - 1];

            //histogram = cummulatedHistogram;

            //skala wykresu 
            scaleOfDiagram = 0;
            for (int i = 0; i < numberOfcolors+1; i++)
            {
                if (cummulatedHistogram[i] > scaleOfDiagram)
                {
                    scaleOfDiagram = cummulatedHistogram[i];
                }
            }
            //np. scaleOfDiagram = 410 (410 wystąpien jakiegoś odcienia) to 410 trzeba przeskalować na 100 pixeli wysokości
            scaleOfDiagram = scaleOfDiagram / bitmapHeigth;
            //1 wartość mnożymy jako jakąś skalę

            for (int i = 0; i < numberOfcolors + 1; i++)//szerokosc
            {
                //columnHeigth = (histogram[i] * 100) / sumOfValues;
                columnHeigth = (cummulatedHistogram[i]) / scaleOfDiagram;
                for (int j = 0; j < columnHeigth; j++)//wysokosc 
                {
                    histogramBitmap.SetPixel(i, (99 - j), Color.FromArgb(0, 0, 0));
                }
            }

        }

        public void ComputeHistogramR() 
        {
            Color color;
            int number;
            int numberOfcolors = this.maxValue;
            int sumOfValues = 0;
            int bitmapHeigth = 100;
            int bitmapWidth = 256;
            histogramBitmap = new Bitmap(bitmapWidth, bitmapHeigth);
            float columnHeigth;
            float scaleOfDiagram;

            if ((this.maxValue == -1) || (this.typeOfFile == 1) || (this.typeOfFile == 4)) //P1, P4, maxVaule = -1
            {
                numberOfcolors = 255;
                histogram = new int[numberOfcolors + 1];
                for (int i = 0; i < this.height; i++)
                    for (int j = 0; j < this.width; j++)
                    {
                        color = bitmap.GetPixel(j, i);
                        number = (int)(color.R);
                        if (number == 255)
                            histogram[0]++;
                        else
                            histogram[255]++;
                        sumOfValues++;
                    }
            }

            if ((this.typeOfFile == 2) || (this.typeOfFile == 5))
            {
                histogram = new int[numberOfcolors + 1];//0-255
                for (int i = 0; i < this.height; i++)
                    for (int j = 0; j < this.width; j++)
                    {
                        color = bitmap.GetPixel(j, i);
                        number = (int)(color.R);
                        histogram[number]++;
                        sumOfValues++;
                    }
            }

            if ((this.typeOfFile == 3) || (this.typeOfFile == 6))
            {
                histogram = new int[numberOfcolors + 1];//0-255
                for (int i = 0; i < this.height; i++)
                    for (int j = 0; j < this.width; j++)
                    {
                        color = bitmap.GetPixel(j, i);
                        number = (int)(color.R); /// dla kanału R
                        histogram[number]++;
                        sumOfValues++;
                    }
            }

            //dla wszystkich typów
            //skala wykresu 
            scaleOfDiagram = 0;
            for (int i = 0; i < numberOfcolors+1; i++)
            {
                if (histogram[i] > scaleOfDiagram)
                {
                    scaleOfDiagram = histogram[i];
                }
            }
            //np. scaleOfDiagram = 410 (410 wystąpien jakiegoś odcienia) to 410 trzeba przeskalować na 100 pixeli wysokości
            scaleOfDiagram = scaleOfDiagram / bitmapHeigth;
            //1 wartość mnożymy jako jakąś skalę

            for (int i = 0; i < numberOfcolors + 1; i++)//szerokosc
            {
                //columnHeigth = (histogram[i] * 100) / sumOfValues;
                columnHeigth = (histogram[i]) / scaleOfDiagram;
                for (int j = 0; j < columnHeigth; j++)//wysokosc 
                {
                    histogramBitmap.SetPixel(i, (99 - j), Color.FromArgb(0, 0, 0));
                }
            }

            
        }

        public void ComputeHistogramG()
        {
            Color color;
            int number;
            int numberOfcolors = this.maxValue;
            int sumOfValues = 0;
            int bitmapHeigth = 100;
            int bitmapWidth = 256;
            histogramBitmap = new Bitmap(bitmapWidth, bitmapHeigth);
            float columnHeigth;
            float scaleOfDiagram;

            if ((this.maxValue == -1) || (this.typeOfFile == 1) || (this.typeOfFile == 4)) //P1, P4, maxVaule = -1
            {
                numberOfcolors = 255;
                histogram = new int[numberOfcolors + 1];
                for (int i = 0; i < this.height; i++)
                    for (int j = 0; j < this.width; j++)
                    {
                        color = bitmap.GetPixel(j, i);
                        number = (int)(color.R);
                        if (number == 255)
                            histogram[0]++;
                        else
                            histogram[255]++;
                        sumOfValues++;
                    }
            }

            if ((this.typeOfFile == 2) || (this.typeOfFile == 5))
            {
                histogram = new int[numberOfcolors + 1];//0-255
                for (int i = 0; i < this.height; i++)
                    for (int j = 0; j < this.width; j++)
                    {
                        color = bitmap.GetPixel(j, i);
                        number = (int)(color.R);
                        histogram[number]++;
                        sumOfValues++;
                    }
            }

            if ((this.typeOfFile == 3) || (this.typeOfFile == 6))
            {
                histogram = new int[numberOfcolors + 1];//0-255
                for (int i = 0; i < this.height; i++)
                    for (int j = 0; j < this.width; j++)
                    {
                        color = bitmap.GetPixel(j, i);
                        number = (int)(color.G); /// dla kanału G
                        histogram[number]++;
                        sumOfValues++;
                    }
            }
            //histopgram skumulowany
            //int[] histcum = new int[256];
            //histcum[o] = hist[0]
            //for int =1, int<256
            //histcum[i] = histcum[i]+histcum[int-1]
            //dla wszystkich typów
            //skala wykresu 
            scaleOfDiagram = 0;
            for (int i = 0; i < numberOfcolors+1; i++)
            {
                if (histogram[i] > scaleOfDiagram)
                {
                    scaleOfDiagram = histogram[i];
                }
            }
            //np. scaleOfDiagram = 410 (410 wystąpien jakiegoś odcienia) to 410 trzeba przeskalować na 100 pixeli wysokości
            scaleOfDiagram = scaleOfDiagram / bitmapHeigth;
            //1 wartość mnożymy jako jakąś skalę

            for (int i = 0; i < numberOfcolors + 1; i++)//szerokosc
            {
                //columnHeigth = (histogram[i] * 100) / sumOfValues;
                columnHeigth = (histogram[i]) / scaleOfDiagram;
                for (int j = 0; j < columnHeigth; j++)//wysokosc 
                {
                    histogramBitmap.SetPixel(i, (99 - j), Color.FromArgb(0, 0, 0));
                }
            }

           
        }

        public void ComputeHistogramB()
        {
            Color color;
            int number;
            int numberOfcolors = this.maxValue;
            int sumOfValues = 0;
            int bitmapHeigth = 100;
            int bitmapWidth = 256;
            histogramBitmap = new Bitmap(bitmapWidth, bitmapHeigth);
            float columnHeigth;
            float scaleOfDiagram;

            if ((this.maxValue == -1) || (this.typeOfFile == 1) || (this.typeOfFile == 4)) //P1, P4, maxVaule = -1
            {
                numberOfcolors = 255;
                histogram = new int[numberOfcolors + 1];
                for (int i = 0; i < this.height; i++)
                    for (int j = 0; j < this.width; j++)
                    {
                        color = bitmap.GetPixel(j, i);
                        number = (int)(color.R);
                        if (number == 255)
                            histogram[0]++;
                        else
                            histogram[255]++;
                        sumOfValues++;
                    }
            }

            if ((this.typeOfFile == 2) || (this.typeOfFile == 5))
            {
                histogram = new int[numberOfcolors + 1];//0-255
                for (int i = 0; i < this.height; i++)
                    for (int j = 0; j < this.width; j++)
                    {
                        color = bitmap.GetPixel(j, i);
                        number = (int)(color.R);
                        histogram[number]++;
                        sumOfValues++;
                    }
            }

            if ((this.typeOfFile == 3) || (this.typeOfFile == 6))
            {
                histogram = new int[numberOfcolors + 1];//0-255
                for (int i = 0; i < this.height; i++)
                    for (int j = 0; j < this.width; j++)
                    {
                        color = bitmap.GetPixel(j, i);
                        number = (int)(color.B); /// dla kanału B
                        histogram[number]++;
                        sumOfValues++;
                    }
            }

            //dla wszystkich typów
            //skala wykresu 
            scaleOfDiagram = 0;
            for (int i = 0; i < numberOfcolors+1; i++)
            {
                if (histogram[i] > scaleOfDiagram)
                {
                    scaleOfDiagram = histogram[i];
                }
            }
            //np. scaleOfDiagram = 410 (410 wystąpien jakiegoś odcienia) to 410 trzeba przeskalować na 100 pixeli wysokości
            scaleOfDiagram = scaleOfDiagram / bitmapHeigth;
            //1 wartość mnożymy jako jakąś skalę

            for (int i = 0; i < numberOfcolors + 1; i++)//szerokosc
            {
                //columnHeigth = (histogram[i] * 100) / sumOfValues;
                columnHeigth = (histogram[i]) / scaleOfDiagram;
                for (int j = 0; j < columnHeigth; j++)//wysokosc 
                {
                    histogramBitmap.SetPixel(i, (99 - j), Color.FromArgb(0, 0, 0));
                }
            }

          
        }

        public void Equalize()
        {
            copyBitmap = new Bitmap(this.bitmap);
            Color color;
            int numberOfpixels = this.height * this.width;
            int colorR, colorG, colorB;
            //wyliczyć cummulatedHistogram
            //for (int i = 0; i < 256; i++)
            //    cummulatedHistogram[i] = histogram[i];

            //cummulatedHistogram[0] = histogram[0];
            //for (int i = 1; i < 256; i++)
            //    cummulatedHistogram[i] = cummulatedHistogram[i] + cummulatedHistogram[i - 1];
            //wyjąć r g b i zachować proporcje lub tylko r
            //przerobic na odcienie szarosci
            
            //wykonać lut
            int index = 0;
            while(cummulatedHistogram[index] <= 0)
                index++;

            for (int i = 0; i < 256; i++)
            {
                //działać na float
                //wykonać lut
                float number = ((float)cummulatedHistogram[i] / (float)numberOfpixels - (float)cummulatedHistogram[index] / (float)numberOfpixels) / (1 - (float)cummulatedHistogram[index] / (float)numberOfpixels) * (255);
                lut[i] = (int)number;
            }

            for (int i = 0; i < this.height; i++)
                for (int j = 0; j < this.width; j++)
                {
                    color = this.bitmap.GetPixel(j, i);
                    if((this.typeOfFile == 2) || (this.typeOfFile == 5))
                    this.bitmap.SetPixel(j, i, Color.FromArgb(lut[(int)color.R],lut[(int)color.R],lut[(int)color.R]));
                    if ((this.typeOfFile == 3) || (this.typeOfFile == 6))
                    {
                        colorR = (int)color.R;
                        colorG = (int)color.G;
                        colorB = (int)color.B;
                        this.bitmap.SetPixel(j, i, Color.FromArgb(lut[colorR], lut[colorG], lut[colorB]));
                    }
                    else 
                    {
                        this.bitmap.SetPixel(j, i, Color.FromArgb(lut[(int)color.R], lut[(int)color.R], lut[(int)color.R]));
                    }
                }

           

        }

        public void Strech()
        {
            int indexMin = 0;
            int indexMax = 255;
            Color color;
            float pixelR, pixelG, pixelB;
            copyBitmap = new Bitmap(this.bitmap);

            while(histogram[indexMin] == 0)
            {
                indexMin++;
            }

            while (histogram[indexMax] == 0)
            {
                indexMax--;
            }

            float scale = 255 / (indexMax - indexMin);

            for (int i = 0; i < this.height; i++)
                for (int j = 0; j < this.width; j++)
                {
                    if ((this.typeOfFile == 2) || (this.typeOfFile == 5))
                    {
                        color = this.bitmap.GetPixel(j, i);
                        pixelR = ((int)color.R - indexMin) * scale;
                        if (pixelR < 0)
                            pixelR = 0;
                        this.bitmap.SetPixel(j, i, Color.FromArgb((int)pixelR, (int)pixelR, (int)pixelR));
                    }
                    else 
                    {
                        color = this.bitmap.GetPixel(j, i);
                        pixelR = ((int)color.R - indexMin) * scale;
                        if (pixelR < 0)
                            pixelR = 0;
                        pixelG = ((int)color.G - indexMin) * scale;
                        if (pixelG < 0)
                            pixelG = 0;
                        pixelB = ((int)color.B - indexMin) * scale;
                        if (pixelB < 0)
                            pixelB = 0;
                        this.bitmap.SetPixel(j, i, Color.FromArgb((int)pixelR, (int)pixelG, (int)pixelB));
                    }
                }
            
 
        }

        

        public void RGBtoGrayScale() 
        {
            Color color;
            int number;
            copyBitmap = new Bitmap(this.bitmap);

            if ((this.typeOfFile == 3) || (this.typeOfFile == 6))
            {
                for (int i = 0; i < this.height; i++)
                    for (int j = 0; j < this.width; j++)
                    {
                        color = bitmap.GetPixel(j, i);
                        number = (int)(((int)(color.R)) * 0.2989 + ((int)(color.G)) * 0.5870 + ((int)(color.B)) * 0.1140); /// dla kanału L
                        bitmap.SetPixel(j, i, Color.FromArgb(255, number, number, number ));
                    }
            }

        }

        public void RGBtoCMYK()
        {
            Color color;
            float black, cyan, magneta, yellow;
            int r, g, b;
            float r1, g1, b1;
            //kopia do ctrl + z
            copyBitmap = new Bitmap(this.bitmap);
            

            if ((this.typeOfFile == 3) || (this.typeOfFile == 6))
            {
                for (int i = 0; i < this.height; i++)
                    for (int j = 0; j < this.width; j++)
                    {
                        color = bitmap.GetPixel(j, i);
                        //RGB to CMYK
                        black = Math.Min(Math.Min(1 - (float)color.R/255f, 1 - (float)color.G/255f), 1 - (float)color.B/255f);
                        if (black == 1)
                        {
                            cyan = 0;
                            magneta = 0;
                            yellow = 0;
                        }
                        cyan = (1 - (float)color.R/255f - black) / (1 - black);
                        magneta = (1 - (float)color.G/255f - black) / (1 - black);
                        yellow = (1 - (float)color.B/255f - black) / (1 - black); 
                        
                        //CMYK to RGB
                        //cyan = (cyan * (1 - black) + black);
                        //magneta = (magneta * (1 - black) + black);
                        //yellow = (yellow * (1 - black) + black);

                        r1 = (1 - Math.Min(1, cyan * (1 - black) + black));
                        r1 *= 255;
                        r = (int)r1;
                        g1 = (1 - Math.Min(1, magneta * (1 - black) + black));
                        g1 *= 255;
                        g = (int)g1;
                        b1 = (1 - Math.Min(1, yellow * (1 - black) + black));
                        b1 *= 255;
                        b = (int)b1;


                        //bitmap.SetPixel(j, i, Color.FromArgb(255, (int)((1 - yellow) * 255), (int)((1 - magneta) * 255), (int)((1 - cyan) * 255)));
                        bitmap.SetPixel(j,i,Color.FromArgb(255,r,g,b));
                    }
            }
        }


        public void changeBrightness(float value, Bitmap bitmapToBrightness)
        {
            copyBitmap = new Bitmap(this.bitmap);
            bitmapTB = new Bitmap(bitmapToBrightness);
            Color color;
            float r, g, b, scale;

            for (int i = 0; i < this.height; i++)
                for (int j = 0; j < this.width; j++)
                {
                    color = bitmapTB.GetPixel(j, i);
                    r = (float)color.R;
                    g = (float)color.G;
                    b = (float)color.B;
                    scale = value/10;
                    r = r * scale;
                    if (r < 0) r = 0;
                    if (r >255) r = 255;
                    g = g * scale;
                    if (g < 0) g = 0;
                    if (g >255) g = 255;
                    b = b * scale;
                    if (b < 0) b = 0;
                    if (b >255) b = 255;
                    bitmapTB.SetPixel(j, i, Color.FromArgb(255, (int)r, (int)g, (int)b));

                }
        }


        public void changeContrast(float value, Bitmap bitmapToContrast)
        {
            copyBitmap = new Bitmap(this.bitmap);
            bitmapTC = new Bitmap(bitmapToContrast);
            Color color;
            int r, g, b;
            int numberOfpixels = this.width * this.height;
            float scale = value / 100;


            for (int i = 0; i < 256; i++) 
            {
                lutContrast[i] = (int)(scale * (i - 255 / 2) + 255 / 2);
                lutContrast[i] = Math.Min(255, Math.Max(0, lutContrast[i]));
            }


            for (int i = 0; i < this.height; i++)
                for (int j = 0; j < this.width; j++)
                {
                    color = bitmapTC.GetPixel(j, i);
                    r = lutContrast[(int)color.R];
                    g = lutContrast[(int)color.G];
                    b = lutContrast[(int)color.B];
                    bitmapTC.SetPixel(j,i,Color.FromArgb(255, r, g, b));

                }


        }


        public void changeGamma(float value, Bitmap bitmapToGamma)
        {
            copyBitmap = new Bitmap(this.bitmap);
            bitmapTG = new Bitmap(bitmapToGamma);
            Color color;
            int r, g, b;
            int numberOfpixels = this.width * this.height;
            float scale = value / 100;

            //if (scale < 1) scale *= -1;
            //scale = 1.5f; 
            for (int i = 0; i < 256; i++)
            {
                //float temp = (float)Math.Pow((i / 255), (1/scale));
                lutGamma[i] = (int)(255 * Math.Pow(i, scale)/Math.Pow (255, scale -1));
                //lutGamma[i] = (int)( 255 * temp);
                //lutContrast[i] = Math.Min(255, Math.Max(0, lutContrast[i]));
            }
            if (value > 0)
            {
                double normalize = (double)255 / (double)lutGamma[255];
                for (int i = 0; i < 256; i++)
                {
                    lutGamma[i] = (int)(lutGamma[i] * normalize);
                }
            }
            else 
            {
                double normalize = (double)255 / (double)lutGamma[1];
                for (int i = 1; i < 256; i++)
                {
                    lutGamma[i] = (int)(lutGamma[i] * normalize);
                }
                lutGamma[0] = lutGamma[1];
            }


            for (int i = 0; i < this.height; i++)
                for (int j = 0; j < this.width; j++)
                {
                    color = bitmapTG.GetPixel(j, i);
                    r = lutGamma[(int)color.R];
                    g = lutGamma[(int)color.G];
                    b = lutGamma[(int)color.B];
                    bitmapTG.SetPixel(j, i, Color.FromArgb(255, r, g, b));
                    //bitmapTG.SetPixel(j, i, Color.FromArgb(255, (int)Math.Pow((color.R / 255), scale), (int)Math.Pow((color.G / 255), scale), (int)Math.Pow((color.B / 255), scale)));

                }


        }


        public Bitmap OgolnyFiltrUstaw(Double[,] Macierz, Double DzielnikNormalizowalny, int MacierzWidthIHight)
        {
            OgolnyFiltr o = new OgolnyFiltr(Macierz, DzielnikNormalizowalny, MacierzWidthIHight, this.width, this.height, this.BITMAPA);
            this.BitmapFiltr = o.OgolnyFiltrZwrocObrazek();
            return this.BitmapFiltr;
        }

        public Bitmap FiltryJG(int MacierzWidthIHight, bool typWygladzania)
        {
            if (typWygladzania)//jesli true to jednostajny
            {
                Jednostajny j = new Jednostajny(MacierzWidthIHight, this.width, this.height, this.BITMAPA);
                this.BitmapFiltr = j.ZwrocObrazekRozmyty();
                return this.BitmapFiltr;
            }
            else
            {
                Gaus g = new Gaus(MacierzWidthIHight, this.width, this.height, this.BITMAPA);
                this.BitmapFiltr = g.ZwrocObrazekRozmyty();
                return this.BitmapFiltr;
            }

        }

        public Bitmap Mediana(int MacierzWidthIHight)
        {
            this.bitmapOryginal = BITMAPA.GetBitmap();//przetrzymuje orygianlna bitmape
            MedianaSzum g = new MedianaSzum(MacierzWidthIHight, this.width, this.height, this.BITMAPA);
            BITMAPA.WczytajNowaCalaBitmape(g.ZwrocObrazekWyszumiony());
            this.BitmapFiltr = BITMAPA.GetBitmap();//przetrzymuje po filtrze
            return this.BitmapFiltr;
        }
        public Bitmap MorfologiaMatematyczna(int MacierzWidthIHight, int TypMorfologii, Double[,] Macierz)
        {
            BITMAPA.WczytajNowaCalaBitmape(this.bitmapOryginal);//przetrzymuje orygianlna bitmape
            if (TypMorfologii == 1)
            {
                this.bitmapOryginal = BITMAPA.GetBitmap();//przetrzymuje orygianlna bitmape
                Delacja g = new Delacja(MacierzWidthIHight, this.width, this.height, this.BITMAPA, Macierz);
                BITMAPA.WczytajNowaCalaBitmape(g.ZwrocObrazekPoDelacji());

                this.BitmapFiltr = BITMAPA.GetBitmap();//przetrzymuje po filtrze
                return this.BitmapFiltr;
            }
            else if (TypMorfologii == 2)
            {
                this.bitmapOryginal = BITMAPA.GetBitmap();//przetrzymuje orygianlna bitmape
                Erozja g = new Erozja(MacierzWidthIHight, this.width, this.height, this.BITMAPA, Macierz);
                BITMAPA.WczytajNowaCalaBitmape(g.ZwrocObrazekPoErozji());

                this.BitmapFiltr = BITMAPA.GetBitmap();//przetrzymuje po filtrze
                return this.BitmapFiltr;
            }
            else if (TypMorfologii == 3)
            {
                this.bitmapOryginal = BITMAPA.GetBitmap();//przetrzymuje orygianlna bitmape
                Erozja g = new Erozja(MacierzWidthIHight, this.width, this.height, this.BITMAPA, Macierz);
                BITMAPA.WczytajNowaCalaBitmape(g.ZwrocObrazekPoErozji());
                Delacja e = new Delacja(MacierzWidthIHight, this.width, this.height, this.BITMAPA, Macierz);
                BITMAPA.WczytajNowaCalaBitmape(e.ZwrocObrazekPoDelacji());

                this.BitmapFiltr = BITMAPA.GetBitmap();//przetrzymuje po filtrze
                return this.BITMAPA.GetBitmap();
            }
            else
            {
                this.bitmapOryginal = BITMAPA.GetBitmap();//przetrzymuje orygianlna bitmape
                Delacja e = new Delacja(MacierzWidthIHight, this.width, this.height, this.BITMAPA, Macierz);
                BITMAPA.WczytajNowaCalaBitmape(e.ZwrocObrazekPoDelacji());
                Erozja g = new Erozja(MacierzWidthIHight, this.width, this.height, this.BITMAPA, Macierz);
                BITMAPA.WczytajNowaCalaBitmape(g.ZwrocObrazekPoErozji());
                this.BitmapFiltr = BITMAPA.GetBitmap();//przetrzymuje po filtrze
                return this.BITMAPA.GetBitmap();
            }

        }

        internal Image Binaryzacja(float value,Bitmap actualBitmap)
        {
            Binaryzacja b = new Binaryzacja(this.width, this.height, this.BITMAPA);

            this.BitmapFiltr = b.ZwrocObrazekPoBinrazycacji(value);
            return this.BitmapFiltr;
        }

        internal Image BinarizationAlgorythms(int MacierzWidthIHight, int algorytmTyp,double K,double KWR)
        {
            BITMAPA.WczytajNowaCalaBitmape(this.bitmapOryginal);
            if (algorytmTyp == 0)
            {
                this.bitmapOryginal = BITMAPA.GetBitmap();//przetrzymuje orygianlna bitmape
                Niblack g = new Niblack(MacierzWidthIHight, this.width, this.height, this.BITMAPA, K);
                BITMAPA.WczytajNowaCalaBitmape(g.ZwrocObrazekNiblack());
                this.BitmapFiltr = BITMAPA.GetBitmap();
                return this.BitmapFiltr;
            }
            else if (algorytmTyp == 1)
            {
                this.bitmapOryginal = BITMAPA.GetBitmap();//przetrzymuje orygianlna bitmape
                Bernsen g = new Bernsen(MacierzWidthIHight, this.width, this.height, this.BITMAPA, K);
                // BITMAPA.WczytajNowaCalaBitmape(g.ZwrocObrazekNiblack());
                this.BitmapFiltr = g.ZwrocObrazekBernsen();
                return this.BitmapFiltr;
            }
            else
            {
                this.bitmapOryginal = BITMAPA.GetBitmap();//przetrzymuje orygianlna bitmape
                WhiteRohrer g = new WhiteRohrer(MacierzWidthIHight, this.width, this.height, this.BITMAPA, KWR);
                // BITMAPA.WczytajNowaCalaBitmape(g.ZwrocObrazekNiblack());
                this.BitmapFiltr = g.ZwrocObrazekWhiteRohrer();
                return this.BitmapFiltr;
            }
        }
    }
}

